#pragma once
#include <functional>
#include "eventloop.h"
namespace DcAdr
{
    class EventLoop;
    enum class ThreadType : uint8_t;
    class EventCommand
    {
    public:
        void appendEvent(ThreadType type, const std::function<void(void)> &func);
        void appendEventIfIdle(ThreadType type, const std::function<void(void)> &func);

        void quitLoop(ThreadType type);
        ~EventCommand();

        template <class RetType, class... Args>
        void asyncGetData(ThreadType sourceType,
                          std::function<void(const RetType &)> handleDataFunc,
                          ThreadType targetType,
                          std::function<RetType(Args...)> getDataFunc,
                          Args... args)
        {
            auto tmp = [=]()
            {
                RetType ret = getDataFunc(args...); //因为可能涉及到线程,所以不能用完美转发
                auto tmp1 = [=]()
                {
                    handleDataFunc(ret);
                };
                appendEvent(sourceType, tmp1);
            };
            appendEvent(targetType, tmp);
        }
    };
} // namespace DcAdr